<?php 
error_reporting(E_ERROR);
session_start();
require_once('constants_shop.php');
require_once('functions.php');
include('classes/userClass.php');



#conect to shop database	
$shopObj = new mysqlClass();

if(isset($_POST['page_id'])){
		$available_lang= $shopObj->fetch_result("SELECT * FROM languages WHERE active=1");
		foreach($available_lang as $value){
			$params= array($_POST['page_title'.$value['id']],$_POST['editor_'.$value['id']],$_POST['meta_keyword'.$value['id']],$_POST['meta_description'.$value['id']],$_POST['page_id'],$value['id']);
			$shopObj->execute_query("UPDATE site_page_details SET name=?, description=?,meta_description=?,meta_keywords=? WHERE site_page_id=? AND language_id=? ",$params);
		}
		//loguri access pagina
		$targetFolder='../acces_logs/';

		if (!is_dir($targetFolder)) {
			mkdir($targetFolder);
		}
		$content='----'.date("d-m-Y H:i:s").'-----'.' Pagina "'.$_POST['page_title'].' a fost modificata de catre utilizatorul cu contul '.$_SESSION['user_name'].'"\n';	
		$fp = fopen("../acces_logs/{$_SESSION['shop_name']}.txt","a");
		fwrite($fp,$content);
		fclose($fp);	

}
if(isset($_POST['delete_carousel_img'])){
	$params=array('carousel_id'=>$_POST['delete_carousel_img']);
	$shopObj->delete('carousel_details',$params);
	$params=array($_POST['delete_carousel_img']);
	$img= $shopObj->fetch_one("SELECT * FROM carousel WHERE id=?",$params);

	unlink("../../shop/images/{$img['picture']}");
	$params=array('id'=>$_POST['delete_carousel_img']);
	$shopObj->delete('carousel',$params);
}
if(isset($_POST['update_carousel'])){
	$params=array($_POST['value'],$_POST['id'],$_POST['language_id']);
	$sql = $shopObj->execute_query("UPDATE carousel_details SET {$_POST['name']}=? WHERE carousel_id=? AND language_id=?",$params);
	print_r($params);	
}
if(isset($_POST['get_carousel_info'])){
	$details = $shopObj->fetch_result("SELECT * FROM carousel_details WHERE carousel_id='{$_POST['get_carousel_info']}' order by language_id");
	foreach ($details as $key => $value) {
		$data['title_'.$value['language_id']]=$value['title'];
		$data['link_'.$value['language_id']]=$value['link'];
		$data['description_carousel_'.$value['language_id']]=$value['description'];
	}
	echo json_encode($data);
}
if(isset($_POST['delete_picture_page'])){
	$params=array();
	$params['picture_id'] = $_POST['delete_picture_page'];
	$params['site_page_id'] = $_POST['site_page'];
	$shopObj->delete('site_page_pictures',$params);


	$prams= array($_POST['delete_picture_page']);
	$pic = $shopObj->fetch_one("SELECT name FROM picture_files WHERE id=?",$prams);

	unlink("../../shop/".$pic['name']);

	$params =array();
	$params['id']=$_POST['delete_picture_page'];
	$shopObj ->delete('picture_files',$params);

}
?>